package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ROLE_COMMENT;
import static xj.model.uml.UMLModelConstants.ROLE_CONSTRAINT;
import static xj.model.uml.UMLModelConstants.ROLE_NOTE;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import xj.meta.AssociationElement;
import xj.meta.ModelElement;

public abstract class ConstrainedAssociationElement 
  extends AssociationElement {

  public ConstrainedAssociationElement() {
    super();
  }

  public ConstrainedAssociationElement(ModelElement left, ModelElement right) {
    super(left, right);
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    ConstrainedAssociationElement be = 
      (ConstrainedAssociationElement) super.clone();
    be.notes = new HashSet<Note>();
    Iterator<Note> iter1 = notes.iterator();
    while (iter1.hasNext()) {
      Note n = iter1.next();
      be.addNote((Note) n.clone());
    }

    be.constraints = new HashSet<Constraint>();
    Iterator<Constraint> iter2 = constraints.iterator();
    while (iter2.hasNext()) {
      Constraint c = iter2.next();
      be.addConstraint((Constraint) c.clone());
    }

    be.comments = new HashSet<Comment>();
    Iterator<Comment> iter3 = comments.iterator();
    while (iter3.hasNext()) {
      Comment c = iter3.next();
      be.addComment((Comment) c.clone());
    }
    return be;
  }

  public Set<Constraint> getConstraints() {
    return constraints;
  }

  public void addConstraint(Constraint constraint) {
    if (constraint != null) {
      constraints.add(constraint);
      addOwnedElement(constraint, ROLE_CONSTRAINT);
    }
  }

  public void removeConstraint(Constraint constraint) {
    if (constraint != null) {
      constraints.remove(constraint);
      removeOwnedElement(constraint);
    }
  }

  public void setConstraint(String s) {
    addConstraint(new Constraint(s));
  }

  public String getConstraint() {
    if (constraints != null) {
      Iterator<Constraint> iter = constraints.iterator();
      if (iter.hasNext()) {
	return iter.next().getBody();
      }
    }
    return null;
  }

  public Set<Comment> getComments() {
    return comments;
  }

  public void addComment(Comment comment) {
    if (comment != null) {
      comments.add(comment);
      addOwnedElement(comment, ROLE_COMMENT);
    }
  }

  public void removeComment(Comment comment) {
    if (comment != null) {
      comments.remove(comment);
      removeOwnedElement(comment);
    }
  }

  public void setComment(String s) {
    addComment(new Comment(s));
  }

  public String getComment() {
    if (comments != null) {
      Iterator<Comment> iter = comments.iterator();
      if (iter.hasNext()) {
	return iter.next().getText();
      }
    }
    return null;
  }

  public Set<Note> getNotes() {
    return notes;
  }

  public void addNote(Note note) {
    if (note != null) {
      notes.add(note);
      addOwnedElement(note, ROLE_NOTE);
    }
  }

  public void removeNote(Note note) {
    if (note != null) {
      notes.remove(note);
      removeOwnedElement(note);
    }
  }

  @Override
  protected boolean inCDATASection(String name) {
    return asElement(name);
  }

  protected Set<Constraint> constraints = new HashSet<Constraint>();

  protected Set<Comment> comments = new HashSet<Comment>();

  protected Set<Note> notes = new HashSet<Note>();

}
